package cn.clwxy.gdz.util;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.*;

public class JsonSerializeHelper {
	/// <summary>
		/// JSON�����л�
		/// </summary>
		public static Object JsonDeserialize(String jsonString, Type type)
		{
			try{
				GsonBuilder builder = new GsonBuilder(); 
	
				// Register an adapter to manage the date types as long values 
				builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
				   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
					   Integer time = json.getAsInt();
					   //SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
						Date date = new Date(time);
						return date;
				   } 
				});
	
				Gson gson = builder.create();
				return gson.fromJson(jsonString, type);
			}
			catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}
		
		/// <summary>
		/// JSON���л�
		/// </summary>
		public static String JsonSerializer(Object t)
		{
			try{
				Gson gson = new GsonBuilder()
			    .excludeFieldsWithModifiers(Modifier.STATIC,Modifier.TRANSIENT, Modifier.VOLATILE)
			    .create();
				return gson.toJson(t);
			}
			catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}
}
